import {createRouter, createWebHistory} from "vue-router"
import {isLoggingApi} from '@/apis/loginApi'
// import {useUserStore} from '@/stores/user'
// const userStore=useUserStore()
//一级功能导入
import Login from "@/views/Login/login.vue"
import Layout from "@/views/Layout/layout.vue"
import index from "@/views/channel/index.vue"
import order from "@/views/channel/order.vue"

import takeOut from "@/views/channel/takeOut.vue"
import inStore from "@/views/channel/inStore.vue"
import product from "@/views/channel/product.vue"
import user from "@/views/channel/user.vue"
import finance from "@/views/channel/finance.vue"
import fitment from "@/views/channel/fitment.vue"
import shop from "@/views/channel/shop.vue"
import data from "@/views/channel/data.vue"
import app from "@/views/channel/app.vue"
import setting from "@/views/channel/setting.vue"
import staff from "@/views/channel/staff.vue"

const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes: [
        {
            path: "/",
            component: Layout,
            children: [
                {path: "/index", component: index},
                {path: "/order", component: order},
                {path: "/inStore", component: inStore},
                {path: "/product", component: product},
                {path: "/user", component: user},
                {path: "/finance", component: finance},
                {path: "/fitment", component: fitment},
                {path: "/fitment", component: fitment},
                {path: "/shop", component: shop},
                {path: "/data", component: data},
                {path: "/app", component: app},
                {path: "/setting", component: setting},
                {path: "/staff", component: staff},
                {path: "/takeOut", component: takeOut},
            ],
        },
        {
            path: "/login",
            component: Login,
        },
    ],
})
router.beforeEach(async (to, from, next) => {
    if (sessionStorage.getItem('omlineStatus')) {
        return next()
    }
    const res = await isLoggingApi()
    if (res.code === 0) {
        sessionStorage.setItem('omlineStatus', JSON.stringify('logging'))
    }
    next()
})


// router.beforeEach((to, from, next) => {
//   const functionTreeStore = useFunctionTreeStore();
//   functionTree.value = functionTreeStore.functionTreeInfo;
//   console.log('路由器获取到功能树：')
//   console.log(functionTree.value)

//   // 动态生成一级功能路由
//   const dynamicRoutes = functionTree.value.map(item => ({
//     path: `/${item.path}`,
//     component: () => import(`@/views/channel/${item.fname}.vue`),
//     props: true,
//   }));

//   //清除旧的动态子路由
//   router.getRoutes().forEach(route => {
//     if (route.name === 'Layout') {
//       route.children = route.children.filter(child => child.name !== 'LayoutChild');
//     }
//   });

//   // 添加新的动态子路由
//   router.addRoute('Layout', {
//     name: 'LayoutChild',
//     path: '/',
//     component: Layout,
//     children: dynamicRoutes,
//   });

//   next();
// });

export default router;
